package com.burgeon.backend.SHOPBACKEND.controller;

import com.alibaba.fastjson.JSONObject;
import com.burgeon.backend.SHOPBACKEND.entity.vip.Employee;
import com.burgeon.backend.SHOPBACKEND.entity.vip.Vip;
import com.burgeon.backend.SHOPBACKEND.service.VipService;
import com.burgeon.backend.SHOPBACKEND.service.WxUserService;
import com.burgeon.backend.Util.menu.CommonUtil;
import com.burgeon.backend.Util.wechatUtil.RedisUtil2;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@Controller
@CrossOrigin
@RequestMapping("weixin/users")
public class WxUserController extends BaseController{
    @Autowired
    private WxUserService wxUserService;
    @Autowired
    private VipService vipService;

    @Resource
    private RedisUtil2 redisUtil2;

    @ResponseBody
    @RequestMapping(value = "/findVipByOpenId")
    public ResponseResult<Vip> findVipByOpenId(HttpSession session) {

        System.err.println("findVipByOpenId");
        String openId = session.getAttribute("openId").toString();
        System.err.println(openId);
        String daoId = redisUtil2.get("daoId");
        System.err.println("redis缓存"+daoId);

        Vip data = vipService.findVipByOpenId(openId,daoId);
        System.err.println("controller==="+data);
        //先用openid查询，是否已有关联
        //1.用手机号去查erp,是否有卡号，有的话再去查是否有openid

        return new ResponseResult<>(SUCCESS,data);
    }
    @ResponseBody
    @RequestMapping(value = "/FindEmployName")
    public ResponseResult<Employee> FindEmployName(HttpSession session){

        String daoId = redisUtil2.get("daoId");
        Employee data = vipService.FindEmployNameFindEmployName(daoId);

        return new ResponseResult<>(SUCCESS,data);
    }



    /**
     * 从微信认证获取微信与erp的信息
     * @param phone
     * @param name
     * @param sex
     * @param birth
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertVip")
    public ResponseResult<String> insertVip(String phone, String name, String sex, String birth, HttpSession session){

        System.err.println(phone);
        String openId = session.getAttribute("openId").toString();
        System.err.println(openId);
         String daoId = redisUtil2.get("daoId");
        System.err.println("redis缓存"+daoId);

        String code = vipService.getVipByPhone(phone,name,sex,birth,openId,daoId);
       //先用openid查询，是否已有关联
        //1.用手机号去查erp,是否有卡号，有的话再去查是否有openid

        return new ResponseResult<>(SUCCESS,code);
    }

    /**
     * 网页授权获取用户openid
     * @Title: getOpenId
     * @param @param code
     * @throws
     */
    @RequestMapping(value = "getOpenId", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult<String>  getOpenId(@RequestParam("code") String code,HttpSession session) {

       // System.err.println(code);
        //通过code获取openId
        JSONObject jsonDate = CommonUtil.getOpenId(code);
      //  System.err.println(jsonDate);
        String openId = "";
        if (null == jsonDate.get("errcode")) {
            openId = jsonDate.getString("openid");
           // System.err.println(openId);
            session.setAttribute("openId",openId);
           // System.err.println(session.getAttribute("openId")+"获取openId，存到缓存");
            String daoId = redisUtil2.get("daoId");
           // System.err.println("redis缓存"+daoId);

           // return new ResponseResult<String> (SUCCESS,openId);
        }
       return new ResponseResult<String> (SUCCESS,openId);
    }

    /**
     * 获取导购的微信用户列表
     * @param daoId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getWxUsers")
    public ResponseResult<ArrayList<Vip>> WxMessage(String daoId) throws Exception {
        ArrayList<Vip> weiXinUsers = wxUserService.getUserByOpenId(daoId);
        return new ResponseResult<ArrayList<Vip>>(SUCCESS,weiXinUsers);
    }

    /**
     * 获取导购的微信用户信息
     * @param cardNo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getWxUserInfo")
    public ResponseResult<Vip> WxUserInfo(String cardNo){
        Vip weiXinUsers = wxUserService.getUserInfoByOpenId(cardNo);
        return new ResponseResult<Vip>(SUCCESS,weiXinUsers);
    }


}
